<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>{0}</title>
</head>

<body>
  Loading...
  <script>
    var
      loc = window.location, url = loc.href, b = document.body,
      isNW = (!window.nw || url.indexOf('chrome-extension://') < 0) ? 0 : 1,
      fileName = url.split('\/').pop(),
      baseURL = url.replace(fileName, '').replace('file:///', '').replace(/\//g, '\\'),
      uri = isNW ? '' : 'http://{1}:{2}/{3}/{4}';  // http://127.0.0.1:5555/wwj/A.html

    if (isNW || url === uri) {
      loc.href = baseURL + 'html/main.html?isNW=' + isNW + '&baseURL=' + encodeURIComponent(baseURL);
    } else {
      const
        msg = [
          '准备中...',  // xhr.UNSENT
          '连接中......',  // xhr.OPENED
          '连接成功.........',  // xhr.HEADERS_RECEIVED
          '接受数据中............',  // xhr.LOADING
          '接受数据完毕！',   // xhr.DONE
          '服务貌似没有正常启动。Windows请双击A.vbs，其他操作系统请命令行执行： node A.js' // error
        ],
        xhr = new XMLHttpRequest();
      // TODO: 再3次尝试，或尝试刷新一定时间仍然error
      // xhr.onerror = e => b.innerText = msg[5];
      xhr.onerror = (e) => {
        b.innerText = msg[5]
      };
      xhr.ontimeout = e => b.innerText = '请求超时！请确认网络并重新刷新浏览器再次尝试！';
      xhr.onreadystatechange = e => {
        b.innerText = '正在检测服务：' + msg[s = xhr.readyState];
        if (s == 4) {
          if (xhr.status == 200) {
            setTimeout(() => {
              b.innerText = '服务正常！正在跳转......';
              setTimeout(e => loc.href = uri, 1000);
              return;
            }, 200);

            // TODO: 再3次尝试，或尝试刷新一定时间仍然error
            b.innerText = msg[5];
          }
        };
      }
      xhr.timeout = 10000;
      xhr.open('GET', uri);
      xhr.send();
    }
  </script>
</body>

</html>